﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Dtos;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Services;
using Microsoft.EntityFrameworkCore;
using Wz.Interface;
using Wz.Entities;
using Wz.Dto;

namespace Wz.Services
{
    public class ChargeTableService: ApplicationService,IChargeTableService
    {
        private readonly IRepository<ChargeTable, Guid>  _chargeTables;
        public ChargeTableService(IRepository<ChargeTable, Guid> _chargeTabless)
        {
            _chargeTables = _chargeTabless;
        }
        //应收费添加
        [HttpPost]
        public async Task<ChargeTableDto> AddChargeTable([FromForm]AddChargeTableDto obj)
        {
                Random aa = new Random();
               ChargeTable item = new ChargeTable(
               GuidGenerator.Create(),  //Guid
               obj.BusinessNum="Ys"+DateTime.Now.ToString("yyyyMMddHHmm")+aa.Next(0,9).ToString(),
               obj.Company,
               obj.PayType,
               obj.Weight,
               obj.Price,
               obj.Btime,
               obj.Cname,
               obj.CreateTime = DateTime.Now,
               obj.Remarks,
               obj.State,
               obj.CheckName,
               obj.CheckTime = DateTime.Now
                );;
            //调用添加方法
            var result = await _chargeTables.InsertAsync(item);

            return ObjectMapper.Map<ChargeTable, ChargeTableDto>(result);
        }
        //应收费单删
        [HttpDelete]
        public async Task ChargeTableDel(Guid id)
        {
            await _chargeTables.DeleteAsync(id);
        }
        //应收费回显
        [HttpGet]
        public async Task<ChargeTableDto> ChargeTableFind(Guid id)
        {
            var result = await _chargeTables.FindAsync(id);
            return ObjectMapper.Map<ChargeTable, ChargeTableDto>(result);
        }
        ////应收费显示
        [HttpGet]
        public async Task<PagedResultDto<ChargeTableDto>> ChargeTableShow(PagedAndSortedResultRequestDto input)
        {
            //创建查询
            var query = _chargeTables.OrderBy(input.Sorting);
            //获取总条数
            var totalCount = await query.CountAsync();

            //获取实体
            List<ChargeTable> ChargeTables= await query
                .ToListAsync();

            //实体映射Dto
            List<ChargeTableDto> ChargeTableDtos =
                ObjectMapper.Map<List<ChargeTable>, List<ChargeTableDto>>(ChargeTables);

            //返回结果
            return new PagedResultDto<ChargeTableDto>(totalCount, ChargeTableDtos);
        }
        //应收费修改
        [HttpPut]
        public async Task<ChargeTableDto> UptChargeTable([FromForm]UptChargeTableDto obj)
        {
            Random aa = new Random();
            ChargeTable item = new ChargeTable(
              obj.ID,  //Guid
              obj.BusinessNum = "Ys" + DateTime.Now.ToString("yyyyMMddHHmm") + aa.Next(0, 9).ToString(),
              obj.Company,
              obj.PayType,
              obj.Weight,
              obj.Price,
              obj.Btime,
              obj.Cname,
              DateTime.Now,
              obj.Remarks,
              obj.State,
              obj.CheckName,
              obj.CheckTime
               );
            //调用添加方法
            var result = await _chargeTables.UpdateAsync(item);

            return ObjectMapper.Map<ChargeTable, ChargeTableDto>(result);
        }
    }
}
